package com.floreantpos.ui.dialog;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import com.floreantpos.IconFactory;
import com.floreantpos.POSConstants;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.swing.PosButton;
import com.floreantpos.ui.TitlePanel;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

public class TelephoneSelectionDialog extends POSDialog implements ActionListener {

  private TitlePanel titlePanel;
  private javax.swing.JFormattedTextField tfNumber;
  private String userEntry = "";
  private String formattedNumber;
  private int userEntryLenght = 0;

  public TelephoneSelectionDialog() {
    setResizable(false);

    Container contentPane = getContentPane();

    MigLayout layout = new MigLayout("fillx", "[60][60][60]", "");
    contentPane.setLayout(layout);

    titlePanel = new TitlePanel();
    contentPane.add(titlePanel, "span, grow, wrap, height 60");

    tfNumber = new JFormattedTextField(createFormatter("(##) *####-####"));
    tfNumber.setFont(tfNumber.getFont().deriveFont(Font.BOLD, 24));
    tfNumber.setEditable(false);
    tfNumber.setBackground(Color.WHITE);
    tfNumber.setHorizontalAlignment(JTextField.RIGHT);
    tfNumber.setText("(00) 00000-0000");
    contentPane.add(tfNumber, "span 2, grow");

    PosButton posButton = new PosButton(POSConstants.CLEAR_ALL);
    posButton.addActionListener(this);
    contentPane.add(posButton, "grow,shrink,wrap, height 55");

    String[][] numbers = {{"7", "8", "9"}, {"4", "5", "6"}, {"1", "2", "3"}, {" ", "0", " "}};
    String[][] iconNames = new String[][]{{"7_32.png", "8_32.png", "9_32.png"}, {"4_32.png", "5_32.png", "6_32.png"}, {"1_32.png", "2_32.png", "3_32.png"},
    {"empty_32.png", "0_32.png", "empty_32.png"}};

    for (int i = 0; i < numbers.length; i++) {
      for (int j = 0; j < numbers[i].length; j++) {
        posButton = new PosButton();
        ImageIcon icon = IconFactory.getIcon(iconNames[i][j]);
        String buttonText = String.valueOf(numbers[i][j]);

        if (icon == null) {
          posButton.setText(buttonText);
        } else {
          posButton.setIcon(icon);
        }

        posButton.setActionCommand(buttonText);
        posButton.addActionListener(this);
        String constraints = "grow, height 55";
        if (j == numbers[i].length - 1) {
          constraints += ", wrap";
        }
        contentPane.add(posButton, constraints);
      }
    }
    contentPane.add(new JSeparator(), "newline, grow, span, gaptop 20");

    posButton = new PosButton(POSConstants.OK);
    posButton.addActionListener(this);
    contentPane.add(posButton, "skip 1, grow, height 55");

    posButton = new PosButton(POSConstants.CANCEL);
    posButton.addActionListener(this);
    contentPane.add(posButton, "grow, height 55");

  }

  private void doOk() {
    setCanceled(false);
    dispose();
  }

  private void doCancel() {
    setCanceled(true);
    dispose();
  }

  private void doClearAll() {
    userEntryLenght = 0;
    userEntry = "";
    formattedNumber = "";
    tfNumber.setText("(00) 00000-0000");
  }

  private void doInsertNumber(String number) {

    userEntry = userEntry + number;
    userEntryLenght = userEntry.length();

    if (userEntryLenght == 1) {
      formattedNumber = "(" + userEntry + "0) 00000-0000";
    } else if (userEntryLenght == 2) {
      formattedNumber = "(" + userEntry + ") 00000-0000";
    } else if (userEntryLenght == 3) {
      formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.charAt(2) + "0000-0000";
    } else if (userEntryLenght == 4) {
      formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 4) + "000-0000";
    } else if (userEntryLenght == 5) {
      formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 5) + "00-0000";
    } else if (userEntryLenght == 6) {
      formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 6) + "0-0000";
    } else if (userEntryLenght == 7) {
      formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 7) + "-0000";
    } else if (userEntryLenght == 8) {
      formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 7) + "-" + userEntry.charAt(7) + "000";
    } else if (userEntryLenght == 9) {
      formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 7) + "-" + userEntry.substring(7, 9) + "00";
    } else if (userEntryLenght == 10) {
      formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 7) + "-" + userEntry.substring(7, 10) + "0";
    } else if (userEntryLenght == 11) {
      formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 7) + "-" + userEntry.substring(7, 11);
    }

    tfNumber.setText(formattedNumber);

  }

  public void actionPerformed(ActionEvent e) {
    String actionCommand = e.getActionCommand();

    if (POSConstants.CANCEL.equalsIgnoreCase(actionCommand)) {
      doCancel();
    } else if (POSConstants.OK.equalsIgnoreCase(actionCommand)) {
      doOk();
    } else if (actionCommand.equals(POSConstants.CLEAR_ALL)) {
      doClearAll();
    } else {
      doInsertNumber(actionCommand);
    }

  }

  public void setTitle(String title) {
    titlePanel.setTitle(title);
    super.setTitle(title);
  }

  public String getValue() {
    return tfNumber.getText();
  }

  public void setValue(String value) {
    tfNumber.setText(value);
  }

  public static void main(String[] args) {
    TelephoneSelectionDialog telephoneDialog = new TelephoneSelectionDialog();
    telephoneDialog.pack();
    telephoneDialog.setVisible(true);
  }

  protected MaskFormatter createFormatter(String s) {
    MaskFormatter formatter = null;
    try {
      formatter = new MaskFormatter(s);
    } catch (Exception exc) {
      MessageDialog.showError(com.floreantpos.POSConstants.INVALID_PHONE + exc.getMessage());
    }
    return formatter;
  }

}
